package com.lou1052.core.serializer;

import com.lou1052.core.constant.tlocal.ThreadLocalKryo;
import com.lou1052.core.util.serialization.KryoSerializerUtil;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created with IntelliJ IDEA.
 * User: liuqing
 * Date: 2015/10/18
 * Time: 16:00
 * To change this template use File | Settings | File Templates.
 */
public class KryoSerializer  implements Serializer{
    public static final String NAME="kryo";

    @Override
    public String name() {
        return NAME;
    }

    @Override
    public byte[] serData(Object object) {
        return ThreadLocalKryo.LOCAL_POOL_KRYO.get().writeObject(object);
    }

    @Override
    public <T> T deserData(byte[] data, Class<T> clazz) {
        return ThreadLocalKryo.LOCAL_POOL_KRYO.get().readObject(data,clazz);
    }

    @Override
    public <T> T readData(InputStream stream, Class<T> clazz) {
        return ThreadLocalKryo.LOCAL_POOL_KRYO.get().readObject(stream,clazz);
    }

    @Override
    public void writeData(OutputStream stream, Object toWrite) {
        ThreadLocalKryo.LOCAL_POOL_KRYO.get().writeObject(stream,toWrite);
    }

    @Override
    public byte[] serObject(Object object) {
        return ThreadLocalKryo.LOCAL_POOL_KRYO.get().writeClassAndObject(object);
    }

    @Override
    public Object deserObject(byte[] data) {
        return ThreadLocalKryo.LOCAL_POOL_KRYO.get().readClassAndObject(data);
    }

    @Override
    public Object readObject(InputStream stream) {
        return ThreadLocalKryo.LOCAL_POOL_KRYO.get().readClassAndObject(stream);
    }

    @Override
    public void writeObject(OutputStream stream, Object toWrite) {
        ThreadLocalKryo.LOCAL_POOL_KRYO.get().writeClassAndObject(stream,toWrite);
    }
}
